<?php 
namespace clt;
class Monthly{
	public $m = [10,20,50,100,200,500,1000];
	/**
     * @var array 缓存的实例
     */
    public $instance = [];
	/**
     * @var object 操作句柄
     */
    public $handler;
    public $type = null;
	/**
     * 自动初始化缓存
     * @access public
     * @param  array $options 配置数组
     * @return Driver
     */
	public function connect($type,$options=[]){
		if (!empty($type) && !isset($this->$instance[$type])) {
            $class = '\\clt\\monthly\\driver\\' . ucwords($type);
            return $this->instance[$type] = new $class($options);
        }
        $this->type = $type;
        $this->instance[$type] = new $class($options);
	}
    public function __construct($name,$options=[])
    {
    	set_time_limit(0);
        if (is_null($this->handler)) {
           
            $this->handler = $this->connect($name,$options=[]);
        }
        $this->getToken();
        return $this->handler;
    }
    /**
     * 获取Token
     * @access public
     * @return bool
     */
    public function getToken()
    {
    	return $this->handler->getToken();
    }
    /**
     * 获取地点
     * @access public
     * @return bool
     */
    public function getLocations(){
    	return $this->handler->getLocations();
    }
    /**
     * 获取通道
     * @access public
     * @return bool
     */
    public function newPortPrice($blocationId,$sleep=10,$term=null){
        return $this->handler->newPortPrice($blocationId,$sleep,$term);
    }
    /**
     * 获取通道
     * @access public
     * @return bool
     */
    public function getPorts(){
    	return $this->handler->getPorts();
    }
    /**
     * 购买
     * @access public
     * @return bool
     */
    public function Buy($data){
        return $this->handler->Buy($data);
    }
    /**
     * 购买
     * @access public
     * @return bool
     */
    public function BuyPort($data){
        return $this->handler->BuyPort($data);
    }
    /**
     * 购买
     * @access public
     * @return bool
     */
    public function BuyVXC($data){
        return $this->handler->BuyVXC($data);
    }
    /**
     * 获取价钱
     * @access public
     * @return bool
     */
    public function getPrice($blocationId,$sleep){
    	return $this->handler->getPrice($blocationId,$sleep);
    }
    public function getCloudFastPrice($blocationId,$sleep){
        $total = $this->getPrice($blocationId,$sleep);
        if($total<=0){
            return false;
        }
        $port['rmb12'] = round($total*1.5);
        $port['usd12'] = round(($total/6.5)*1.5);
        $port['rmb6'] = round($total*1.5*1.2);
        $port['usd6'] = round(($total/6.5)*1.5*1.2);
        $port['rmb3'] = round($total*1.5*1.2*1.3);
        $port['usd3'] = round(($total/6.5)*1.5*1.2*1.3);
        $port['rmb1'] = round($total*1.5*1.2*1.3*1.4);
        $port['usd1'] = round(($total/6.5)*1.5*1.2*1.3*1.4);
        return $port;
    }
    /**
     * 获取价钱
     * @access public
     * @return bool
     */
    public function getAllPrice($m=[]){
    	$data= $this->handler->getAllPrice($this->m);
    	return $this->export($data['items'],$data['headers'],$data['sheetnName']);
    }
    /**
     * 导出到表格中
     * @access public
     * @return bool
     */
    public function export($data,$headers=[],$sheetnName='WORK'){
    	header("Content-type: text/html; charset=utf-8");
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能import导入
        import("Excel.PHPExcel",PLUGIN_PATH);
        import("Excel.PHPExcel.Writer.Excel2007",PLUGIN_PATH);
        import("Excel.PHPExcel.Writer.Excel5",PLUGIN_PATH);
        import("Excel.PHPExcel.IOFactory",PLUGIN_PATH);
        import("Excel.PHPExcel.CSV",PLUGIN_PATH);
        $date = date("Y-m-d", time());
        $fileName = $this->type." Monthly ";
        $fileName .= date("Y-m-d", time());
        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();
        //设置表头
        $key = ord("A");
        $headIndex = 1;
        $objActSheet = $objPHPExcel->getActiveSheet();
        
        $key = ord("A");
        foreach ($headers as $v) {
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . $headIndex, $v);
            if($colum=='A' || $colum=='I' || $colum=='J'){
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($colum)->setWidth(15);
            }else{
                $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($colum)->setWidth(25);
            }
            
            $key += 1;
        }
        $column = $headIndex+1;
        foreach ($data as $key => $rows) { //行写入
        	foreach($rows['items'] as $mb=>$val){
        		$span = ord("A");
	         	foreach ($val as $keyName => $value) {// 列写入
		           $j = chr($span);
		           $objActSheet->setCellValue($j . $column, $value);
		           $span++;
		        }
	            $column++;
        	}
        }
       
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->getActiveSheet()->setTitle($sheetnName); 
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        //header('Content-Type: application/vnd.ms-excel');
        //header("Content-Disposition: attachment;filename=\"$fileName\"");
        //header('Cache-Control: max-age=0');
        //$objWriter->save('php://output'); //文件通过浏览器下载
        $filepath = 'uploads/excel/'.date('Ymd',time())."/";
        if(!is_dir($filepath)){
            make_dir($filepath);
        }
        $filepath .=time().".xlsx";
        $objWriter->save($filepath); //文件通过浏览器下载
        return $filepath;
    }
     /**
     * 获取流量使用情况
     * @access public
     * @return bool
     */
    public function getUsage($condition){
        return $this->handler->getUsage($condition);
    }
}
?>